﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using MINERP.DAL;
using MINERP.InfoConfig.Controller;
using DevExpress.XtraEditors.Controls;

namespace MINERP.InfoConfig
{
    public partial class FrmUserRoleManager : FrmMDIChildFormTemplate
    {

        private UserRoleController      _controller = null;
        private tb_Power                _power = null;

        static FrmUserRoleManager instance;
        private FrmUserRoleManager()
        {
            InitializeComponent();
        }

        public static FrmUserRoleManager CreateInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmUserRoleManager();
                }
                return instance;
            }
        }
     
        private void FrmUserRoleManager_Load(object sender, EventArgs e)
        {
            //绑定gridView
            _controller = new UserRoleController();
            _controller.Grid = gridControlUserRole;
            _controller.View = gridViewUserRole;
            _controller.GetDate();

            _controller.CheckedComboBoxEdit = chkcmbTech;
            _controller.LinkchkcmbTech();

        }

        private void gridViewUserRole_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _power = (tb_Power)gridViewUserRole.GetRow(e.FocusedRowHandle);

            if (_power != null)
            {
                if (!_power.tb_Tech.IsLoaded)
                {
                    _power.tb_Tech.Load();
                }
                txtRoleName.Text = _power.Name;
                txtRoleDes.Text = _power.Des;
                txtFormName.Text = _power.FrmName;
                txtAssemblyName.Text = _power.AssemblyName;

                List<tb_Tech> TechNameList = _power.tb_Tech.ToList();
                List<string> temlist = new List<string>();
                foreach (var item in TechNameList)
                {
                    temlist.Add(item.TechName);
                }
                _controller.CheckedComboBoxEdit = chkcmbTech;
                _controller.LinkchkcmbTech();
                foreach (CheckedListBoxItem item in chkcmbTech.Properties.Items)
                {
                    if (temlist.Contains(item.Value.ToString()))
                    {
                        item.CheckState = CheckState.Checked;
                    }
                }
                _controller.GroupControl = groupControlTech;
                _controller.CreateConctrolsByPower(_power);
            }

        }
        //添加
        private void btnAdd_Click(object sender, EventArgs e)
        {
             if (btnAdd.Text != "保存")
            {
                PrepareAdd(true);
                txtRoleName.Focus();
                return;
            }
            if (CheckData())
            {
                tb_Power obj = new tb_Power();
                obj.Name = txtRoleName.Text;
                obj.Des = txtRoleDes.Text;
                obj.FrmName = txtFormName.Text;
                obj.AssemblyName = txtAssemblyName.Text;
                _controller.AddRole(obj);

                //_controller = new UserRoleController();
                _controller.Grid = gridControlUserRole;
                _controller.View = gridViewUserRole;
                _controller.GetDate();

                PrepareAdd(false);
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
        /// <summary>
        /// 更新角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (btnUpdate.Text != "保存")
            {
                PrepareUpdate(true);
                txtRoleName.Focus();
                return;
            }
            if (CheckData())
            {
                _power.Name = txtRoleName.Text;
                _power.Des = txtRoleDes.Text;
                _power.FrmName = txtFormName.Text;
                _power.AssemblyName = txtAssemblyName.Text;

                _controller.UpdateRole(_power);

                //_controller = new UserRoleController();
                _controller.Grid = gridControlUserRole;
                _controller.View = gridViewUserRole;
                _controller.GetDate();
                PrepareUpdate(false);

                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            } 
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
            {
                _controller.DeleteRole(_power);

                //_controller = new UserRoleController();
                _controller.Grid = gridControlUserRole;
                _controller.View = gridViewUserRole;
                _controller.GetDate();
            }
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            txtRoleName.Enabled = false;
            txtRoleDes.Enabled = false;
            txtFormName.Enabled = false;
            txtAssemblyName.Enabled = false;
            chkcmbTech.Enabled = false;

            btnAdd.Text = "添加";
            btnUpdate.Text = "更新";
            btnAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }
        /// <summary>
        /// 检查数据完整性
        /// </summary>
        /// <returns></returns>
        private bool CheckData()
        {
            bool flag = true;
            if (txtRoleName.Text == string.Empty)
            {
                txtRoleName.Focus();
                MessageBox.Show("用户角色不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (txtRoleDes.Text == string.Empty)
            {
                txtRoleDes.Focus();
                MessageBox.Show("角色描述不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (txtFormName.Text == string.Empty)
            {
                txtFormName.Focus();
                MessageBox.Show("窗体名称不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            if (txtAssemblyName.Text == string.Empty)
            {
                txtAssemblyName.Focus();
                MessageBox.Show("程序集不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return flag;
        }
        /// <summary>
        /// 添加前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareAdd(bool bl)
        {
            txtRoleName.Enabled = bl;
            txtRoleDes.Enabled = bl;
            txtFormName.Enabled = bl;
            txtAssemblyName.Enabled = bl;
            chkcmbTech.Enabled = bl;


            txtRoleName.Text = string.Empty;
            txtRoleDes.Text = string.Empty;
            txtFormName.Text = string.Empty;
            txtAssemblyName.Text = string.Empty;


            if (bl)
            {

                btnAdd.Text = "保存";
                btnUpdate.Enabled = false;
                btnDelete.Enabled  = false;
            }
            else
            {
                btnAdd.Text = "添加";
                btnUpdate.Enabled = true;
                btnDelete.Enabled = true;
            }
        }
      
        /// <summary>
        /// 更新前控件设置
        /// </summary>
        /// <param name="bl"></param>
        private void PrepareUpdate(bool bl)
        {
            txtRoleName.Enabled = bl;
            txtRoleDes.Enabled = bl;
            txtFormName.Enabled = bl;
            txtAssemblyName.Enabled = bl;
            chkcmbTech.Enabled = bl;

            if (bl)
            {

                btnUpdate.Text = "保存";
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
            }
            else
            {
                btnUpdate.Text = "更新";
                btnAdd.Enabled = true;
                btnDelete.Enabled = true;
            }
        }

        private void chkcmbTech_Closed(object sender, DevExpress.XtraEditors.Controls.ClosedEventArgs e)
        {
            _controller.GroupControl = groupControlTech;
            _controller.CreateConctrolsOfSelectTechs();
        }

        private void FrmUserRoleManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }




    }
}
